/**
 * Created by meiguizhinian on 2018/6/21
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

let LLScene = cc.Scene.extend({
	ctor : function(form, space=null) {
		this._super();
		this._spaceName = space;
		this._formName = form;
		this._formNode = null;

		this._teachingLayer = null;

		LLResourceManager.sharedInstance().switchSpace(this._spaceName);
		LLResourceManager.sharedInstance().attach(this, this._formName);
		this._formNode = ivy.UIManager.getInstance().popUpFormByName(this._formName);
	},

	onEnter : function() {
		this._super();
		//注册按键监听
		this.setKeypadEnabled(true);
	},
	onExit : function() {
		//注销按键监听
		this.setKeypadEnabled(false);
		this._super();
	},

	advertiseReward : function(success, rewardId) {
		cc.log("当前无广告奖励奖励回调");
	},

	showDialog : function(dialog) {
		this.addChild(dialog, LLScene.LLLayerZorder.kLLLayerZorderDialog);
	},

	closeDialog : function() {
		let children = this.getChildren();
		for (let i=0; i!==children.length; ++i) {
			let child = children[i];
			if (child instanceof LLDialog) {
				child.close();
				return true;
			}
		}
		return false;
	},

	showTouchShieldLayer : function() {
		let layer = new cc.LayerColor(cc.color(255,255,255,0));
		this.addChild(layer, LLScene.LLLayerZorder.kLLLayerZorderShieldTouch);
		let listener = cc.EventListener.create({
			event : cc.EventListener.TOUCH_ONE_BY_ONE,
			swallowTouches : true,
			onTouchBegan : function () {
				cc.log("触摸屏蔽");
			    return true;
			}
		});
		cc.eventManager.addListener(listener, layer);
	},

	showRatingLayer : function() {
		let layer = LLRating.create();
		this.enableTouchClose(layer);
		this.addChild(layer, LLScene.LLLayerZorder.kLLLayerZorderRating);
	},

	showTeachingLayer : function() {
		// 裁剪节点
		if (!this._teachingLayer) {
			this._teachingLayer = LLTeaching.create();
			this._teachingLayer.setOnExitCallback(function () {
				this._teachingLayer = null;
			}.bind(this));
			this.addChild(this._teachingLayer, LLScene.LLLayerZorder.kLLLayerZorderTeaching);
		}
	},
	hideTeachingLayer : function() {
		if (this._teachingLayer) {
			this._teachingLayer.removeFromParent();
			this._teachingLayer = null;
		}
	},
	////////////////////////////////////////////////////////////////////////////////
	enableTouchClose : function(node) {
		//设置点击到时候删除自己
		let listener = cc.EventListener.create({
			event : cc.EventListener.TOUCH_ONE_BY_ONE,
			swallowTouches : true,
			onTouchBegan : function (touch, event) {
				let node = event.getCurrentTarget();
				let location = node.getParent().convertToNodeSpace(touch.getLocation());
				return node.getBoundingBox().containsPoint(location);
			},
			onTouchEnded : function (touch, event) {
				let node = event.getCurrentTarget();
				node.removeFromParent();
			}
		});
		cc.eventManager.addListener(listener, node);
	},

	onKeyReleased : function(keyCode, event) {
		if (keyCode===cc.KEY.back || keyCode===cc.KEY.escape) {
			//advertise 10.点击手机返回键，调用退出接口
			if (!this.closeDialog()) {
				Advertise.onQuit();
			}
		}
	},

	_spaceName : null,
	_formName : null,
	_formNode : null,
	_teachingLayer : null,
});

LLScene.LLLayerZorder = {
	kLLLayerZorderDialog : 200,
	kLLLayerZorderRating : 201,
	kLLLayerZorderTeaching : 202,
	kLLLayerZorderShieldTouch : 203,
	kLLLayerZorderAdvertise : 204,
};
